package com.emode.action;

import java.util.List;
import java.util.Set;

import com.emode.base.BaseAction;
import com.emode.entity.SysGroup;
import com.emode.entity.SysPrivilege;
import com.opensymphony.xwork2.ActionContext;

public class GroupAction extends BaseAction<SysGroup>{

	/**
	 * 
	 */
	private static final long serialVersionUID = 7771446942641355652L;
	private Integer pid;
	
	/*
	 * 显示用户组的所有信息
	 * 显示用户组信息可以显示用户组所拥有的权限
	 */
	public String groupList(){
		List<SysGroup> groups = groupservice.getGroupByPage(model, p);
		
		ActionContext.getContext().put("group", groups);
		List<SysPrivilege> prilis = privilegeservice.findAll();
		ActionContext.getContext().put("privi", prilis);
		ActionContext.getContext().put("privi2", prilis);
		return "grouplist";
		
	}
	
	/*
	 * 添加用户组
	 * 用户组与权限关系表示由group来维护的，所以每次插入group都需要给group分配权限
	 * 即多对多插入
	 */
	public String addGroup(){
		groupservice.save(model);
		SysPrivilege privilege = new SysPrivilege();
		privilege.setPid(pid);
		System.out.println("pid:"+pid);
		model.getPrivilege().add(privilege);
		groupservice.update(model);
		return "togrouplist";
	}
	/*
	 * 更新用户组
	 * 用户组每更新都需要update 一次
	 * update涉及到用户权限的更新
	 */
	public String updateGroup(){
		return null;
		
	}
	/*
	 * s删除用户组，
	 * 当需要删除用户组是，改用户组下的用户权限就会变更，怎样删除用户组，这是个问题
	 */
	public String delGroup(){
		System.out.println(model.getGroupid());
		if(model.getGroupid()==null){
			return null;
		}
		SysGroup group  = groupservice.getById(model.getGroupid());
		System.out.println("group is :"+group);
		Set<SysPrivilege> privi = group.getPrivilege();
		System.out.println("privilege is:"+privi);
		for(SysPrivilege p:privi){
			System.out.println(p);
			group.getPrivilege().remove(p);
		}
		groupservice.deleteByIds(model.getGroupid());
		return "togrouplist";
		
	}

	public Integer getPid() {
		return pid;
	}

	public void setPid(Integer pid) {
		this.pid = pid;
	}
	
	

}
